﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BBS.Bll;
using BBS.Bll.Extend;
using BBS.Bll.Forum;
using BBS.Bll.Sys;
using Rule = BBS.Entity.Rule;
using BBS.Entity;

namespace BBS.install
{
    public partial class Install : System.Web.UI.Page
    {
        readonly Bll.Sys.User _user = new User();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindPoints();
                BindAdmin();
                BindAccountLevel();
                BindBoardList();
            }
        }

        protected void BtnInitData_Click(object sender, EventArgs e)
        {
            Entity.Rule rule = new Rule();
            rule.DeletePoints = TxtDelPoints.Text.ToInt();
            rule.HighlightPoints = TxtHighlightPoints.Text.ToInt();
            rule.LockPoints = TxtLockPoints.Text.ToInt();
            rule.LoginPoints = TxtLoginPoints.Text.ToInt();
            rule.MovePoints = TxtMovePoints.Text.ToInt();
            rule.PostPoints = TxtPostPoints.Text.ToInt();
            rule.ReplyPoints = TxtReplyPoints.Text.ToInt();
            rule.TopPoints = TxtTopPoints.Text.ToInt();
            SystemInfo.InsertOrUpdateRule(rule);
            BindPoints();
            
        }

        private void BindPoints()
        {
            var rule = SystemInfo.GetForumRuleParameter();
            LblTopPoints.Text = rule.TopPoints.ToString();
            LblReplyPoints.Text = rule.ReplyPoints.ToString();
            LblPostPoints.Text = rule.PostPoints.ToString();
            LblMovePoints.Text = rule.MovePoints.ToString();
            LblLoginPoints.Text = rule.LoginPoints.ToString();
            LblLockPoints.Text = rule.LockPoints.ToString();
            LblHighlightPoints.Text = rule.HighlightPoints.ToString();
            LblDelPoints.Text = rule.DeletePoints.ToString();
        }

        private void BindAdmin()
        {
            try
            {
                var account = _user.LogIn("zachary.woo@163.com", "123");
                LblEmail.Text = account.Email;
            }
            catch (Exception)
            {
               
            }
        }

        private void BindAccountLevel()
        {
            GridAccountLevel.DataSource = SystemInfo.GetUserLevelSetting();
            GridAccountLevel.DataBind();            
        }

        private void BindBoardList()
        {
            var list = BoardInfo.GetAllBoard();
            DdlForum.DataSource = list;
            DdlForum.DataBind();
            DdlForum.Items.Insert(0, new ListItem("新建版块", "-1"));

            DdlBoard.DataSource = BoardInfo.GetAllBoard();
            DdlBoard.DataBind();
        }

        protected void BtnAddAdmin_Click(object sender, EventArgs e)
        {
            Account account = new Account();
            account.Type = AccountType.Administrator;
            account.Sign = "I'm an administrator";
            account.RegisterTime = DateTime.Now;
            account.QQ = "149331387";
            account.Points = 0;
            account.Password = "123";
            account.NickName = "管理员";
            account.LastTime = DateTime.Now;
            account.LastIp = Request.UserHostAddress;
            account.Gender = true;
            account.Email = "zachary.woo@163.com";
            account.Birthday = new DateTime(1983, 9, 29);
            account.MSN = string.Empty;
            _user.CreateUser(account);
            BindAdmin();
        }

        protected void BtnAddPointsName_Click(object sender, EventArgs e)
        {
            AccountLevel level = new AccountLevel();
            level.Points = TxtNum.Text.ToInt();
            level.Title = TxtPointsName.Text;
            SystemInfo.InsertAccountLevel(level);
            BindAccountLevel();
        }

        protected void BtnUpdateBoard_Click(object sender, EventArgs e)
        {
            var board = new Board();
            board.Order = TxtForumOrder.Text.ToInt();
            board.ParentId = DdlForum.SelectedValue.ToInt();
            board.Information = TxtForumInformation.Text;
            board.Title = TxtForumName.Text;
            board.Status = StatusType.Normal;
            BoardInfo.InsertBoard(board);
            BindBoardList();
            
        }

        protected void BtnCheck_Click(object sender, EventArgs e)
        {
            bool b = VerificationCode.CheckCode(TxtVerificationCode.Text, HttpContext.Current);
            if (b)
            {
                LblCheck.Text = "正确";
            }
            else
            {
                LblCheck.Text = "错误";
            }
        }

        protected void BtnThreadType_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(DdlBoard.SelectedValue))
            {
                ThreadType threadType = new ThreadType();
                threadType.Title = TxtThreadType.Text;
                threadType.LastUpdateTime = DateTime.Now.ToString();
                threadType.Status = StatusType.Normal;
                threadType.LastUpdateBy = 1;//need login user accountId
                threadType.BoardId = DdlBoard.SelectedValue.ToInt();
                ProcessThreadType(BoardInfo.InsertThreadType, threadType);
            }            
        }
        protected void BtnUpdateThreadType_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(DdlBoard.SelectedValue) && !string.IsNullOrEmpty(DdlThreadType.SelectedValue))
            {
                ThreadType threadType = new ThreadType();
                threadType.Id = DdlThreadType.SelectedValue.ToInt();
                threadType.BoardId = DdlBoard.SelectedValue.ToInt();
                threadType.LastUpdateTime = DateTime.Now.ToString();
                threadType.Status = StatusType.Normal;
                threadType.LastUpdateBy = 1;//need login user accountId
                threadType.Title = TxtThreadType.Text;
                ProcessThreadType(BoardInfo.UpdateThreadType, threadType);
            }
        }
        #region process threadType
        delegate  void ProcessThreadTypeDelegate(ThreadType type);

        private void ProcessThreadType(ProcessThreadTypeDelegate d, ThreadType type)
        {
            if (!string.IsNullOrEmpty(DdlBoard.SelectedValue))
            {
                d(type);
                ClientScript.RegisterClientScriptBlock(GetType(), "key", "alert('操作成功')", true);
            }
            else
            {
                ClientScript.RegisterClientScriptBlock(GetType(), "key", "alert('先选择板块')", true);
            }
        }
        #endregion
        protected void DdlBoard_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(DdlBoard.SelectedValue))
            {
                DdlThreadType.DataSource = BoardInfo.GetThreadTypeByBoardId(int.Parse(DdlBoard.SelectedValue));
                DdlThreadType.DataBind();
            }
        }
    }
}
